'''
Created on Jul 21, 2010

@author: riche
'''

from ft.framework import application, component
from ft.framework.net_io import *
from ft.directory import localhost

class RouteAbstractionComp(component.Implementation):
    
    def handle_request(self, msg):
        print('Routing this: ' + msg) 
        self.send_out('out', (localhost.AGREE0_NAME,msg))

class Route(application.Application):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        application.Application.__init__(self)
        
    def setup(self):
        
        route_comp = RouteAbstractionComp('route_comp')
        self.create_component(route_comp, [('in', 'handle_request'), ('out', '__OUT__')])
        
        nr = net_reader.NetReaderComponent(self._node_name, localhost.local_directory)
        nw = net_writer.NetWriterComponent('netout', self._node_name, localhost.local_directory)
        
        self.add_components([nr, nw])
        
        nr_to_route = self.create_connection((self._node_name, 'out'), ('route_comp', 'in'))
        route_to_nw = self.create_connection(('route_comp', 'out'),('netout','in'))

        nr_thread = application.CompThread(nr, [nr_to_route], True)
        nw_thread = application.CompThread(nw, [route_to_nw])
        route_thread = application.CompThread(route_comp, [nr_to_route, route_to_nw])

        self.add_threads([nr_thread, nw_thread, route_thread])
        
if __name__ == '__main__':
    route = Route()
    route.parse_opts()
    route.setup()
    route.execute()
